package com.ruoyi.hxf.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.hxf.domain.Comment;
import com.ruoyi.hxf.domain.Topic;
import com.ruoyi.hxf.service.ICommentService;
import com.ruoyi.hxf.service.ITopicService;
import com.ruoyi.system.service.ISysUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 评论管理
 *
 * @author hxf
 * @date 2023-1-22
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/topic/comment")
public class CommentController extends BaseController {
    private final ICommentService iCommentService;
    private final ISysUserService iSysUserService;
    private final ITopicService iTopicService;
    /**
     * 查询帖子对应的评论管理列表
     */
    @GetMapping("/AppList")
    public TableDataInfo<Comment> list(Comment comment, PageQuery pageQuery) {
        TableDataInfo<Comment> commentTableDataInfo = iCommentService.selectPageCommentList(comment, pageQuery);
        Long userId = LoginHelper.getUserId();
        for (Comment row : commentTableDataInfo.getRows()) {
            row.setRemark(iSysUserService.selectUserByUserName(row.getCreateBy()).getAvatar());
            row.setCreateBy(iSysUserService.selectUserByUserName(row.getCreateBy()).getNickName());
            if ("1".equals(iCommentService.selectCollectionByUserIdAndCommentId(userId, row.getCommentId(),"1"))) {
                row.setTopicCollection(iCommentService.selectCollectionByUserIdAndCommentId(userId, row.getCommentId(),"1"));
            } else {
                row.setTopicCollection("0");
            }
        }
        return commentTableDataInfo;
    }



    /**
     * 新增评论信息
     */
    @Log(title = "新增帖子信息", businessType = BusinessType.INSERT)
    @PostMapping
    public R<Comment> add(@Validated @RequestBody Comment comment) {
        iCommentService.insertComment(comment);
        Topic topic = iTopicService.queryById(String.valueOf(comment.getTopicId()));
        topic.setTopicCount(topic.getTopicCount()+1);
        topic.setUpdateBy(null);
        topic.setUpdateTime(null);
        iTopicService.updateByTopic(topic);
        PageQuery pageQuery=new PageQuery();
        TableDataInfo<Comment> commentTableDataInfo = iCommentService.selectPageCommentList(comment,pageQuery);
        Comment comment1 = commentTableDataInfo.getRows().get(Math.toIntExact(0));
        comment1.setRemark(iSysUserService.selectUserByUserName(comment1.getCreateBy()).getAvatar());
        comment1.setCreateBy(iSysUserService.selectUserByUserName(comment1.getCreateBy()).getNickName());
        return R.ok(comment1);
    }

    /**
     * 喜欢和取消喜欢
     */
    @Log(title = "喜欢和取消喜欢", businessType = BusinessType.UPDATE)
    @PutMapping("/{commentId}/{userId}/{status}/{type}")
    public R<Void> likeEdit(@PathVariable("commentId") String commentId, @PathVariable("userId") String userId, @PathVariable("status") String status,@PathVariable("type") String type) {
        if(iCommentService.selectCommentLike(commentId,String.valueOf(LoginHelper.getUserId()),type)){
            iCommentService.updateCommentLike(commentId, String.valueOf(LoginHelper.getUserId()), status,type);
        }else{
            iCommentService.insertCommentLike(commentId, String.valueOf(LoginHelper.getUserId()), status,type);
        }
        Comment comment = iCommentService.queryById(commentId);
        if(status.equals("1")){
            comment.setCommentLikes(comment.getCommentLikes() + 1);
        }else{
            comment.setCommentLikes(comment.getCommentLikes() - 1);
        }
        return toAjax(iCommentService.updateByComment(comment));
    }
}
